#!/bin/bash
# Usage: ./push_multiarch.sh <base_image> <target_repo>
# Example: ./push_multiarch.sh golang:1.24.4 artifacts.lenovo.com/cybertron-dev

set -e  # 出现任何错误立即退出

if [ $# -ne 2 ]; then
    echo "Usage: $0 <base_image> <target_image>"
    echo "Example: $0 golang:1.24.4 artifacts.lenovo.com/cybertron-dev/golang:1.24.4"
    exit 1
fi

BASE_IMAGE=$1
TARGET_IMAGE=$2


# 处理AMD64架构
process_architecture() {
    local ARCH=$1
    echo "Processing ${ARCH} architecture..."
    
    # 拉取镜像
    #docker pull --platform linux/${ARCH} ${BASE_IMAGE}
    
    # 打标签
    local TARGET_TAG="${TARGET_IMAGE}-${ARCH}"
    docker tag ${BASE_IMAGE} ${TARGET_TAG}
    
    # 推送镜像
    docker push ${TARGET_TAG}
    
    echo "${ARCH} image pushed successfully."
}

# 处理两种架构
# process_architecture amd64
process_architecture arm64

# 创建并推送manifest
MANIFEST_TAG="${TARGET_IMAGE}"
echo "Creating manifest for ${MANIFEST_TAG}"

docker manifest create ${MANIFEST_TAG} \
    # --amend ${TARGET_IMAGE}-amd64 \
    --amend ${TARGET_IMAGE}-arm64

docker manifest push ${MANIFEST_TAG}
echo "Manifest pushed successfully."

echo "All operations completed!"


docker manifest create artifacts.lenovo.com/cybertron-dev/ascendai_pytorch_jupyter:1.0 --amend artifacts.lenovo.com/cybertron-dev/ascendai_pytorch_jupyter:1.0-arm64